package seata.order.service;

import io.seata.core.context.RootContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import seata.order.feign.UserFeignClient;

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {
	final UserFeignClient userFeignClient;
	final JdbcTemplate jdbcTemplate;

	public void create(String userId, String commodityCode, Integer count) {
		log.info("Order Service ... xid: " + RootContext.getXID());
		int orderMoney = count * 100;
		jdbcTemplate.update("insert order_tbl(user_id,commodity_code,count,money) values(?,?,?,?)",
				userId, commodityCode, count, orderMoney);

		userFeignClient.reduce(userId, orderMoney);
	}
}